package com.kingdee.eas.custom.common;

import java.math.BigDecimal;

public class MathUtils {

	public static BigDecimal roundHalf(BigDecimal v) {
		if (v == null) {
			return BigDecimal.ZERO;
		}
		BigDecimal a = null;
		BigDecimal b = null;
		BigDecimal r = null;
		a = v.setScale(0, BigDecimal.ROUND_FLOOR);

		b = v.subtract(a);

		if (b.compareTo(BigDecimal.valueOf(0.5)) > 0) {
			r = a.add(BigDecimal.ONE);
		} else if (b.compareTo(BigDecimal.valueOf(0.5)) < 0) {
			r = a;
		} else {
			r = v;
		}
		return r;
	}

	public static BigDecimal roundUp(BigDecimal v, int newScale) {
		return v.setScale(newScale, BigDecimal.ROUND_UP);
	}

	public static double round025(double param) {
		return ((int) (param / 0.25)) * 0.25;
	}

	public static double round05(double param) {
		return ((int) (param / 0.5)) * 0.5;
	}

	public static BigDecimal avoidNull(Object obj) {
		if (obj == null) {
			return BigDecimal.ZERO;
		} else {
			return new BigDecimal(obj.toString());
		}
	}
}
